<template>
  <div v-loading="loading">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px" class="dataForm">
      <el-row>
        <el-col :span="12">
          <el-form-item :label="$t('交易日期')" prop="transactionDate" :rules="[{required: true, message: $t('必填信息'), trigger: ['blur', 'change']}]">
            <el-date-picker
              v-model="form.transactionDate"
              clearable
              style="width: 200px"
              type="date"
              value-format="yyyy-MM-dd"
              :placeholder="$t('交易日期')"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('交易后剩余数量（股）')" prop="transactionAfterNumber" :rules="[{required: true, message: $t('必填信息'), trigger: ['blur', 'change']}]">
            <Money v-model="form.transactionAfterNumber" :placeholder="$t('交易后剩余数量')" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('交易数量（股）')" prop="transactionNumber" :rules="[{required: true, message: $t('必填信息'), trigger: ['blur', 'change']}]">
            <Money v-model="form.transactionNumber" :placeholder="$t('交易数量')" @change="onChnage($event,'1')" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('交易单价（元/股）')" prop="transactionPrice" :rules="[{required: true, message: $t('必填信息'), trigger: ['blur', 'change']}]">
            <Money v-model="form.transactionPrice" :placeholder="$t('交易单价')" @change="onChnage($event,'1')" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('交易金额（元）')" prop="transactionAmount" :rules="[{required: true, message: $t('必填信息'), trigger: ['blur', 'change']}]">
            <Money v-model="form.transactionAmount" :placeholder="$t('交易金额')" @change="onChnage($event,'1')" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('手续费（元）')" prop="commission" :rules="[{required: true, message: $t('必填信息'), trigger: ['blur', 'change']}]">
            <Money v-model="form.commission" :placeholder="$t('手续费')" @change="onChnage($event,'1')" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('印花税（元）')" prop="stampDuty" :rules="[{required: true, message: $t('必填信息'), trigger: ['blur', 'change']}]">
            <Money v-model="form.stampDuty" :placeholder="$t('印花税')" @change="onChnage($event,'1')" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('过户费（元）')" prop="transferFee" :rules="[{required: true, message: $t('必填信息'), trigger: ['blur', 'change']}]">
            <Money v-model="form.transferFee" :placeholder="$t('过户费')" @change="onChnage($event,'1')" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('资金收入金额（元）')" prop="incomeAmount" :rules="[{required: true, message: $t('必填信息'), trigger: ['blur', 'change']}]">
            <Money v-model="form.incomeAmount" :placeholder="$t('资金收入金额')" @change="onChnage($event,'1')" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('抵消成本（元）')" prop="offsetCosts" :rules="[{required: true, message: $t('必填信息'), trigger: ['blur', 'change']}]">
            <Money v-model="form.offsetCosts" :placeholder="$t('抵消成本')" @change="onChnage($event,'1')" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('抵消公允价值变动损益（元）')" prop="offsetLosses" :rules="[{required: true, message: $t('必填信息'), trigger: ['blur', 'change']}]">
            <Money v-model="form.offsetLosses" :placeholder="$t('抵消公允价值变动损益')" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('增值税及附加（元）')" prop="valueAddedTax" :rules="[{required: true, message: $t('必填信息'), trigger: ['blur', 'change']}]">
            <Money v-model="form.valueAddedTax" :placeholder="$t('增值税及附加')" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('财务顾问费（元）')" prop="financialAdvisoryFees" :rules="[{required: true, message: $t('必填信息'), trigger: ['blur', 'change']}]">
            <Money v-model="form.financialAdvisoryFees" :placeholder="$t('财务顾问费')" @change="onChnage($event,'1')" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('计入投资收益（元）')" prop="investIncome" :rules="[{required: true, message: $t('必填信息'), trigger: ['blur', 'change']}]">
            <Money v-model="form.investIncome" :placeholder="$t('计入投资收益')" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('收益率（%）(财务口径)')" prop="yield" :rules="[{required: true, message: $t('必填信息'), trigger: ['blur', 'change']}]">
            <Money v-model="form.yield" :placeholder="$t('收益率')" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer" style="text-align: right;">
      <!--保存 取消按钮-->
      <save-btn has-permi="transactionRecords:records" />
    </div>
  </div>
</template>

<script>
    import { saveDealSpoTransactionRecords, getDealSpoTransactionRecords } from '@/api/deal/spo/transaction/dealSpoTransactionRecords'
    export default {
        data() {
            return {
                // 表单参数
                form: {},
                // 遮罩层
                loading: false,
                // 表单校验
                rules: {
                }
            }
        },
        created() {
        },
        methods: {
            // 初始化数据
            init(data) {
                this.loading = true
                if (data != null && data.id !== undefined) {
                    getDealSpoTransactionRecords(data.id).then(response => {
                        this.form = response.data
                        this.loading = false
                    }).catch(() => {
                      this.loading = false
                    })
                } else {
                    this.reset()
                    this.loading = false
                }
            },
            // 表单重置
            reset() {
                this.form = {
                    id: undefined,
                    dealId: this.$route.query.id,
                    reductionId: undefined,
                    transactionDate: undefined,
                    transactionAfterNumber: undefined,
                    transactionNumber: undefined,
                    transactionPrice: undefined,
                    transactionAmount: undefined,
                    commission: undefined,
                    stampDuty: undefined,
                    transferFee: undefined,
                    incomeAmount: undefined,
                    offsetCosts: undefined,
                    offsetLosses: undefined,
                    valueAddedTax: undefined,
                    financialAdvisoryFees: undefined,
                    investIncome: undefined,
                    yield: undefined,
                    createdBy: undefined,
                    createdTime: undefined,
                    updateBy: undefined,
                    updatedTime: undefined
                }
                this.resetForm('form')
            },
            // 取消按钮
            cancel() {
                this.$parent.$parent.formOpen = false
                this.$parent.$parent.reset()
            },
            // 关闭form页面
            closeForm() {
                // 关闭form页面遮罩层
                this.loading = false
                // 关闭form页面
                if (this.$parent.$parent.formOpen !== undefined) {
                  this.$parent.$parent.formOpen = false
                }
                // 刷新list页面
                if (this.$parent.$parent.getList !== undefined) {
                  this.$parent.$parent.getList()
                }
            },
          onChnage(value, type) {
            if (type === '1') {
              this.form.incomeAmount = this.formatNumber(this.form.transactionAmount) - this.formatNumber(this.form.commission) - this.formatNumber(this.form.stampDuty) - this.formatNumber(this.form.transferFee)
              this.form.offsetCosts = this.formatNumber(this.form.transactionNumber) * this.formatNumber(this.form.transactionPrice)
              this.form.valueAddedTax = (this.formatNumber(this.form.transactionAmount) - this.formatNumber(this.form.offsetCosts)) / 1.06 * 0.06
              this.form.investIncome = this.formatNumber(this.form.incomeAmount) - this.formatNumber(this.form.offsetCosts) - this.formatNumber(this.form.valueAddedTax) - this.formatNumber(this.form.financialAdvisoryFees)
              this.form.yield = this.formatNumber(this.form.investIncome) / this.formatNumber(this.form.offsetCosts) * 100
            }
          },
            /** 提交按钮 */
            submitForm() {
                this.$refs['form'].validate(valid => {
                    if (valid) {
                        this.loading = true
                        saveDealSpoTransactionRecords(this.form).then(response => {
                            if (response.code === 200) {
                                this.msgSuccess(this.$t('保存成功'))
                                this.closeForm()
                            } else {
                                this.msgError(response.msg)
                            }
                            this.loading = false
                        })
                    } else {
                       this.locationError(document)
                    }
                }).catch(() => {
                    this.loading = false
                })
           },
          formatNumber(val) {
            if (val === undefined || val === null || val === '' || isNaN(parseFloat(val))) {
              return 0
            }
            return parseFloat(val)
          }
        }
    }
</script>
